<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/templates/common/taglib.jsp"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  <!--   <title>大连大学附属中山医院</title> -->
	<script type="text/javascript" src="${basePath}/static/publics/assets/jquery/jquery.min.js"></script>
	<script type="text/javascript" src="${basePath}/static/publics/assets/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src="${basePath}/static/publics/assets/jqPaginator/jqPaginator.js"></script>
	<script type="text/javascript" src="${basePath}/static/publics/assets/My97DatePicker/WdatePicker.js"></script>

	<style>
	.changeCss{color: red;}
	.changeinputCss{border: 1px red solid;}
	.overlay{z-index:100;position:absolute;top:-138px;left:0px;background-color:#000;opacity:0.5;filter:alpha(opacity=50);display:none;}
	.win{z-index:10000;position:absolute;top:130px;left:60%;width:400px;height:450px;background:#fff;margin:-102px 0 0 -202px;display:none;}
	h2{text-align:right;padding-right: 5px;margin-top: -3px;margin-bottom:10px;}
	h2 span{color:#000000;font-size:25;color:#000000;cursor:pointer;}
	.input_text{
		height: 38px;
		width: 240px;
		margin-bottom: 10px;
		padding-left:2px;
	}
	.input_td{
		padding-left: 38px;
		padding-right:4px;
		text-align:right;
	}
	.input_submit{
	    display:block;
		height: 38px;
		width: 240px;
		line-height:38px;
		background-color: #07a396;
		color: white;
		font-size: 18;
		font-family: 幼圆;
		border:none;
		text-align: center;
	}
</style>
	
</head>
<body>
	<div class="list_NR">
	  <!-- 归档弹框 -->
    <div id="overlay" class="overlay"></div>
	<div id="win" class="win">
	<h2 ><span id="close" onclick="closeOverlay()" >×</span></h2>
	  
		<table>
			<input type="hidden" id="tracementTreaceId" value=""/>
			<input type="hidden" value="" name="first_level_guid"/>
			<tr><td class="input_td">姓　　名:</td><td><input name='patient_name' disabled class="input_text" type="text" title=''></td></tr>
			<tr><td class="input_td">身份证号:</td><td><input name='id_cards' disabled class="input_text" type="text" title=''></td></tr>
			<tr><td class="input_td">ID号:</td><td><input  name='mr_id' disabled class="input_text" type="text" title=''></td></tr>
			<tr><td class="input_td">出院科室:</td><td><input name='outOfficeName' disabled class="input_text" type="text" title=''></td></tr>
			<tr><td class="input_td">出院日期:</td><td><input name='out_hospital_date' disabled class="input_text" type="text" title=''></td></tr>
			<tr><td class="input_td changeCss">上架日期:</td><td><input class="input_text" name="traceDateTime" value="" type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate: '%y-%M-%d'})"></tr>
			<tr><td class="input_td changeCss">上架货位号:</td><td><input class="input_text"  name="shelvedNo" type="text" ></tr>
			<tr><td></td><td id="updateTreamentTraceShelNo"><a class="input_submit" onclick="Complete_Pigeonhole(this)">上架</a></td></tr>
		</table>
	</div>
	
        <div class="loc">
            <h3>病案上架</h3>
          
        </div>
        
        <div class="list_con_table">
         <input type="hidden" value="${medTraceQuery.totalCounts }" id="totalCountsHidden">
			 <form action="${basePath}/statis/queryMedPutAwayByAjax" method="get" id="queryForm">
	        	<input type="hidden" name="currentPage" value="${medTraceQuery.currentPage }" id="currentPageHidden">
	        	<input type="hidden" name="pageSize" value="${medTraceQuery.pageSize }" id="pageSizeHidden">
	        	<div class="search_table">
                <ul class="clearfix" style="margin-bottom:0;">
                 <li class="Label_1" style="width:10%">ID号：&nbsp;</li>
				    <li class="Label_2" style="width:18%"><input class="input_box" type="text" name='mrId' id="mrId" value='${medTraceQuery.mrId}' /></li>
                 
					<li class="Label_1" style="width:11%">住院次数：&nbsp;</li>
				    <li class="Label_2" style="width:22%"><input class="input_box"  type="text"  id="visitNumber" name='visitNumber' value='${medTraceQuery.visitNumber}' /></li>
				 	<li class="Label_1" style="width:11%">出院日期：&nbsp;</li>
                    <li class="Label_2" style="width:28%">
                    	<input class="input_box" style="width:42%;" type="text" name="outHospitalStartDate" value='<fmt:formatDate value="${medTraceQuery.outHospitalStartDate}" pattern="yyyy-MM-dd"/>' onFocus="WdatePicker()" />
                    	<span>-</span>
                    	<input class="input_box" type="text" style="width:42%;" name="outHospitalEndDate" value='<fmt:formatDate value="${medTraceQuery.outHospitalEndDate}" pattern="yyyy-MM-dd"/>' onFocus="WdatePicker()" />
                    </li>
				   <li class="Label_1" style="width:10%">身份证号：&nbsp;</li>
				    <li class="Label_2" style="width:18%"><input class="input_box"  type="text"  name='idCard' value='${medTraceQuery.idCard}' /></li>
				   <li class="Label_1" style="width:11%">上架状态：&nbsp;</li>
				    <li class="Label_2" style="width:22%">
				    
				    	<c:choose>
				    		<c:when test="${medTraceQuery.traceTypeId!=null}">
				    				 <input type="radio"  name='traceTypeId' <c:if test="${medTraceQuery.traceTypeId==2}">checked="checked"</c:if> value='2' />未上架&nbsp;&nbsp;
    					 			<input type="radio"  name='traceTypeId' <c:if test="${medTraceQuery.traceTypeId==12}">checked="checked"</c:if> value='12' />已上架
				    		</c:when>
				    		<c:otherwise>
				    				<input  type="radio"  name='traceTypeId' checked="checked" value='2' />未上架&nbsp;&nbsp;
    					 			<input  type="radio"  name='traceTypeId'  value='12' />已上架
				    		</c:otherwise>
				    	</c:choose>
				    </li>
				   
                   <li class="Label_1" style="width:11%">出院科室：&nbsp;</li>
				    <li class="Label_2" style="width:28%">
				    	<select name="outHospitalOfficeId" class="input_box">
				    		<option value="">请选择</option>
				    		<c:forEach var="outOffice" items="${offices }">
				    			<c:choose>
				    				<c:when test="${ medTraceQuery.outHospitalOfficeId==outOffice.id}">
				    					<option value="${outOffice.id }" selected="selected">${outOffice.name }</option>
				    				</c:when>
				    				<c:otherwise>
				    					<option value="${outOffice.id }">${outOffice.name }</option>
				    				</c:otherwise>
				    			</c:choose>
				    		</c:forEach>
				    	</select>
				    </li>
				  <%--   <li class="Label_1" style="width:10%;">上架单号：&nbsp;</li>
				    <li class="Label_2" style="width:30%"><input class="input_box"  type="text"  name='mrId' value='${medTraceQuery.mrId}' /></li> --%>
				 	   
                    <li class="Label_5">
                        <div class="right" style="text-align:right">
                            <a id="Add" onclick="queryFormSubmit()">查询</a>
                        </div>
                    </li>
                </ul>
            </div>
	        </form>

        </div>
       
        <table class="Search_tableList" cellpadding="0" cellspacing="0">
     
            <tr>
                <td class="tdLabel_4">姓名</td>
                <td class="tdLabel_4">身份证号</td>
                <td class="tdLabel_4">ID号</td>
                <td class="tdLabel_4">住院次数</td>
                 <td class="tdLabel_4">出院科室</td>
                <td class="tdLabel_4">出院日期</td>
                <td class="tdLabel_4">操作</td>
            </tr>
            
           
            <tbody id="tbody">
            	<c:forEach var="treatmentTrace" items="${treatmentTraces }">
            		<tr>
            			<td class='tdLabel_5'>${treatmentTrace.patient_name}</td>
            			<td class='tdLabel_5'>${treatmentTrace.id_cards}</td>
            			<td class='tdLabel_5'>${treatmentTrace.mr_id}</td>
            			<td class='tdLabel_5'>${treatmentTrace.visit_number}</td>
            			<td class='tdLabel_5'>${treatmentTrace.outOfficeName}</td>
            			<td class='tdLabel_5'><fmt:formatDate value="${treatmentTrace.out_hospital_date}" pattern="yyyy-MM-dd"/></td>
                        <td class='tdLabel_5'>
            				<c:choose>
            					<c:when test="${treatmentTrace.traceTypeId==2}">
            						<a class="int" first_guid="${treatmentTrace.treatment_guid}" out_hospital_date="<fmt:formatDate value="${treatmentTrace.out_hospital_date}" pattern="yyyy-MM-dd HH:mm:ss"/>"   outOfficeName="${treatmentTrace.outOfficeName}" mr_id="${treatmentTrace.mr_id}" id_cards="${treatmentTrace.id_cards}"  patientName="${treatmentTrace.patient_name }"   onclick='Pigeonhole(this)'>许可上架</a>
            					</c:when>
            					<c:otherwise>
            						<a class="int" firstGuid="${treatmentTrace.treatment_guid}"  out_hospital_date="<fmt:formatDate value="${treatmentTrace.out_hospital_date}" pattern="yyyy-MM-dd HH:mm:ss"/>"   outOfficeName="${treatmentTrace.outOfficeName}" mr_id="${treatmentTrace.mr_id}" id_cards="${treatmentTrace.id_cards}"  patientName="${treatmentTrace.patient_name }"  onclick="searchTrace(this)">查看病案上架号</a>
            					</c:otherwise>
            				</c:choose>
            			</td>
            			
            		</tr>
            	</c:forEach>
            </tbody>
               
           
        </table>
        <div class="pageList">
            <ul class="pagination clearfix" id="page_plus"></ul>
            <div class="pagination">
                <div>总共：<b id="totalPage">0</b> 条信息    当前页是第 <b id="currentPage">0/0</b>　页</div>
            </div>
        </div>
        
         <form action="${basePath}/statis/queryMedPutAwayByAjax" method="get" id="pageForm">
	        	<input type="hidden" name="currentPage" value="${medTraceQuery.currentPage }" id="currentPageHid">
	        	<input type="hidden" name="pageSize" value="${medTraceQuery.pageSize }" id="pageSizeHid">
	        	<input type="hidden" name="visitNumber" value="${medTraceQuery.visitNumber }">
	        	<input type="hidden" name="name" value="${medTraceQuery.name }">
	        	<input type="hidden" name="mrId" value="${medTraceQuery.mrId }">
	        	<input type="hidden" name="idCard" value="${medTraceQuery.idCard }">
	        	<input type="hidden" name="outHospitalStartDate" value="<fmt:formatDate value="${medTraceQuery.outHospitalStartDate}" pattern="yyyy-MM-dd"/>">
	        	<input type="hidden" name="outHospitalEndDate" value="<fmt:formatDate value="${medTraceQuery.outHospitalEndDate}" pattern="yyyy-MM-dd"/>">
	        	<input type="hidden" name="outHospitalOfficeId" value="${medTraceQuery.outHospitalOfficeId}">
	        	<input type="hidden"  name='traceTypeId' value="${medTraceQuery.traceTypeId}" />
	        </form> 
    </div>
</body>

<script type="text/javascript">
function queryFormSubmit(){

	$("#queryForm").submit();
}

$("#mrId").change(function(){
	var $this=$(this);
	var inentity=$this.val();
	if(inentity!=null && inentity!='' && inentity!=undefined){
		if(isContainMark(inentity)){
			var mrId=inentity.split("-")[0];
			var visitNumber=inentity.split("-")[1];
			
			if(!isNumber(visitNumber)){
				alert("请重新扫描条形码");
				$("#mrId").val("").focus();
				$("#visitNumber").val("");
				
				return;
			}
			//alert(mrId+"--"+visitNumber);
			$("#mrId").val("").val(mrId);
			$("#visitNumber").val("").val(visitNumber);
			$("#queryForm").submit();
		}
	}
});

function isNumber(value) {
    var patrn = /^[0-9]*$/;
    if (patrn.exec(value) == null || value == "") {
        return false
    } else {
        return true
    }
}


function isContainMark(inentity){
	var reg = RegExp(/-/);
	return reg.test(inentity);
}


$(function(){
	$("#mrId").focus();
	var totalCounts = $("#totalCountsHidden").val();
	var pageSize = $("#pageSizeHidden").val();
	var currentPage = $("#currentPageHidden").val();
	initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
});

function initPage(totalCounts,pageSize,currentPage){
	 var visiblePages = 5;
	 var totalPages =  totalCounts%pageSize==0 ? (totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
	 $('#page_plus').jqPaginator({

	        totalCounts: totalCounts,
	        pageSize:pageSize,
	        visiblePages: visiblePages,
	        currentPage: currentPage,

	        first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	        last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
	        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	        onPageChange: function (num,type) {

	     		if(type !='init'){
	     			$("#currentPageHidden").val(num);
					$("#pageSizeHidden").val(pageSize);
					$("#currentPageHid").val(num);
					$("#pageSizeHid").val(pageSize);
					$("#pageForm").submit();
				}
	            $("#totalPage").html(totalCounts);
	            $("#currentPage").html(num+"/"+totalPages);
	            $("#currentPageHidden").val(currentPage);
			    $("#pageSizeHidden").val(pageSize);
	        }
	    });

}

//查看数据病案事件
function scan(obj){
	 var target=$(obj);
	 var guid=target.closest("tr").attr("data-guid");
	// window.open('${corePath}/medical_record/searchHomePageIndexForm?visitGuid='+guid);
	 window.open('${corePath}/medical_record/searchHomePageIndexForm?visitGuid='+guid);
 }

//归档按钮事件
function Pigeonhole(obj){
	target=$(obj);
	var htmlHeight=$('.list_NR').height();
	var htmlWidth=$('.list_NR').width();
	var left = $('html').css('left');
	var top = $('html').css('top');
	$(".overlay").css({
		height:htmlHeight,
		width:htmlWidth,
		left:left,
		top:top
	});
	
    $("#overlay").show();
	$("#win").show();
	
	var patientName=$(obj).attr("patientName");
	//归档日期默认为当前日期
	var date = new Date();  
    var seperator1 = "-";  
    var seperator2 = ":";  
    var month = date.getMonth() + 1;  
    var strDate = date.getDate();  
    if(month >= 1 && month <= 9) {  
        month = "0" + month;  
    }  
    if(strDate >= 0 && strDate <= 9) {  
        strDate = "0" + strDate;  
            }  
    var end = date.getFullYear() + seperator1 + month + seperator1 + strDate +" " + date.getHours() + seperator2 + date.getMinutes() +  
          seperator2 + date.getSeconds();  
    $("input[name='traceDateTime']").val(end);
   // alert($(obj).attr("first_guid"));
    $("input[name='first_level_guid']").val(($(obj).attr("first_guid")==undefined?'':$(obj).attr("first_guid")));
	$("input[name='patient_name']").val((patientName==undefined?'':patientName));
	$("input[name='id_cards']").val(($(obj).attr("id_cards")==undefined?'':$(obj).attr("id_cards")));
	$("input[name='mr_id']").val(($(obj).attr("mr_id")==undefined?'':$(obj).attr("mr_id")));
	$("input[name='outOfficeName']").val(($(obj).attr("outOfficeName")==undefined?'':$(obj).attr("outOfficeName")));
	$("input[name='out_hospital_date']").val(($(obj).attr("out_hospital_date")==undefined?'':$(obj).attr("out_hospital_date")));
}

//病案上架
/* function putOnShelve(guid,obj){
	$.ajax({
		type:"post",
		async:false,
		url:"${basePath}/statis/addPutAway",
		data:{guid:guid},
		success:function(result){
			if(result!=null){
				if(result.success){
					layer.alert("病案许可上架!",{icon:1});
					$("#win input").val("");
					$("#overlay").hide();
					$("#win").hide();
					$(obj).html("已上架")
					//$("#queryForm").submit();
				}
				//layer.alert(result.msg,{icon:1});
			//	layer.alert("请选择要上传的文件夹！", {icon: 0});
			}else{
				//alert();
				layer.alert("病案上架失败",{icon:0});
			}
		}
	});
} */



function Complete_Pigeonhole(){
	var firstGuid = $("input[name='first_level_guid']").val();
	var traceDateTime=$("input[name='traceDateTime']").val();
	var shelvedNo =$("input[name='shelvedNo']").val();

	
	if(!shelvedNo){
		layer.alert("上架货位号不能 为空!",{icon:0});
		return;
	}
	
	
	$.ajax({
		type:"post",
		async:false,
		url:"${basePath}/statis/addPutAway",
		data:{treatmentGuid:firstGuid,traceDate:traceDateTime,traceInfo:shelvedNo},
		success:function(result){
			console.log(result)
			if(result!=null){
				if(result.success){
					layer.alert("病案上架成功!",{icon:1});
					target.closest("td").html("").html("已上架");
					$("#win input").val("");
					$("#overlay").hide();
					$("#win").hide();
				}else{
					layer.alert("病案上架失败",{icon:0});
				}
			}else{
				layer.alert("病案上架失败",{icon:0});
			}
		}
	});
}

//关闭弹框
function closeOverlay(){
	$("#win input").val("");
	$("#overlay").hide();
	$("#win").hide();
}


function searchTrace(obj){
	var guid=$(obj).attr("firstGuid");
	var treamentTreaceId = null;
	var shelvedNo=null;
	$.ajax({
		type:"post",
		data:{guid:guid},
		async:false,
		url:"${basePath}/statis/getTreatmentTraceByCodeAndGuid",
		success:function(data){
			if(data){
				var treatement=data[0];
				treamentTreaceId=treatement.id;
				shelvedNo=treatement.trace_info;
				
			}
		}
		
	});
	
	target=$(obj);
	var htmlHeight=$('.list_NR').height();
	var htmlWidth=$('.list_NR').width();
	var left = $('html').css('left');
	var top = $('html').css('top');
	$(".overlay").css({
		height:htmlHeight,
		width:htmlWidth,
		left:left,
		top:top
	});
	
    $("#overlay").show();
	$("#win").show();
	
	var patientName=$(obj).attr("patientName");
	//归档日期默认为当前日期
	var date = new Date();  
    var seperator1 = "-";  
    var seperator2 = ":";  
    var month = date.getMonth() + 1;  
    var strDate = date.getDate();  
    if(month >= 1 && month <= 9) {  
        month = "0" + month;  
    }  
    if(strDate >= 0 && strDate <= 9) {  
        strDate = "0" + strDate;  
            }  
    var end = date.getFullYear() + seperator1 + month + seperator1 + strDate +" " + date.getHours() + seperator2 + date.getMinutes() +  
          seperator2 + date.getSeconds();  
    $("input[name='traceDateTime']").val(end);
   // alert($(obj).attr("first_guid"));
    $("input[name='first_level_guid']").val(($(obj).attr("first_guid")==undefined?'':$(obj).attr("first_guid")));
	$("input[name='patient_name']").val((patientName==undefined?'':patientName));
	$("input[name='id_cards']").val(($(obj).attr("id_cards")==undefined?'':$(obj).attr("id_cards")));
	$("input[name='mr_id']").val(($(obj).attr("mr_id")==undefined?'':$(obj).attr("mr_id")));
	$("input[name='outOfficeName']").val(($(obj).attr("outOfficeName")==undefined?'':$(obj).attr("outOfficeName")));
	$("input[name='out_hospital_date']").val(($(obj).attr("out_hospital_date")==undefined?'':$(obj).attr("out_hospital_date")));
	$("#tracementTreaceId").val(treamentTreaceId);
	
	$("input[name='shelvedNo']").val(shelvedNo==undefined?'':shelvedNo);
	
	$("#updateTreamentTraceShelNo").html("").html('<a class="input_submit" onclick="update_shelvedNo()">修改上架单号</a>');
}


function update_shelvedNo(){
	var shelvedNo = $("input[name='shelvedNo']").val();
	var traceDateTime=$("input[name='traceDateTime']").val();
	var tracementTreaceId=$("#tracementTreaceId").val();
	if(!shelvedNo){
		layer.alert("上架货位号不能 为空!",{icon:0});
		return;
	}
	//alert(shelvedNo+"--"+traceDateTime+"--"+tracementTreaceId);
	$.ajax({
		type:"post",
		data:{id:tracementTreaceId,traceDate:traceDateTime,traceInfo:shelvedNo},
		url:"${basePath}/statis/updateTreatementTrace",
		success:function(data){
			if(data.success){
				layer.alert("病案信息修改成功!",{icon:1});
				//target.closest("td").prev("td").html("已归档");
				target.closest("td").html("").html("已修改");
				$("#win input").val("");
				$("#overlay").hide();
				$("#win").hide();
			}else{
				
			}
		}
	})
}


</script>